﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cmsservice.Entity.Log;

namespace cmsservice.DAL.Hibernate
{
    public class CLogDAL
    {
        public void Add(CLog c)
        {
            using (LogContext context = new LogContext())
            {
                context.Session.Save(c);
                context.Session.Flush();
            }
        }

        public void Add(string page, string method,string logType, Exception ex, params string[] args)
        {
            try
            {
                CLog c = new CLog();
                StringBuilder sb = new StringBuilder();
                if (args != null && args.Length > 0)
                {
                    for (int i = 0; i < args.Length; i++)
                    {
                        var argObj = args[i];
                        if (argObj == null) argObj = "";
                        sb.AppendLine("参数" + i + ":" + argObj);

                    }
                }
                sb.AppendLine(ex.Message + Environment.NewLine + ex.Source + Environment.NewLine + ex.StackTrace);
                sb = sb.Replace(Environment.NewLine, "<br/>");
                c.Page = page;
                c.Method = method;
                c.LogType = logType;
                c.Content = sb.ToString();
                c.CTime = DateTime.Now;
                Add(c);
            }
            catch
            { }
        }
    }
}
